//
//  NavigationController.swift
//  XMTV
//
//  Created by 王磊 on 17/4/26.
//  Copyright © 2017年 王磊. All rights reserved.
//

import UIKit

class NavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置naviBar背景图片
        UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "navigationbarBackgroundWhite"), for: UIBarMetrics.default)
        // 设置title的字体
        // UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:UIFont.systemFont(ofSize: 20)]
        self.interactivePopGestureRecognizer?.delegate = nil
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        print(self.viewControllers.count)
        if self.viewControllers.count < 1 {
            viewController.navigationItem.rightBarButtonItem = setRightButton()
        }else{
        viewController.hidesBottomBarWhenPushed = true
        viewController.navigationItem.leftBarButtonItem = setBackBarButtonItem()
        }
        super.pushViewController(viewController, animated: true)
    }

    func setRightButton() -> UIBarButtonItem {
        let searchItem = UIButton.init(type: UIButtonType.custom)
        searchItem.setImage(UIImage.init(named: "searchbutton_nor"), for: UIControlState.normal)
        searchItem.sizeToFit()
        searchItem.frame.size = CGSize.init(width: 30, height: 30)
        searchItem.contentHorizontalAlignment = .right
        searchItem.addTarget(self, action: #selector(searchClick), for: UIControlEvents.touchUpInside)
        return UIBarButtonItem.init(customView: searchItem)
    }
    func setBackBarButtonItem() -> UIBarButtonItem {
        let backButton = UIButton.init(type: UIButtonType.custom)
        backButton.setImage(UIImage.init(named: "setting_back"), for: UIControlState.normal)
        backButton.sizeToFit()
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
        backButton.addTarget(self, action: #selector(backClick), for: UIControlEvents.touchUpInside)
        return UIBarButtonItem.init(customView: backButton)
    }
    /// 返回
    func backClick() {
        self.popViewController(animated: true)
    }
    /// 点击右边的搜索
    func searchClick() {
//        let searchvc = SearchVC()
//        self.pushViewController(searchvc, animated: true)
    }
}
